/**
 * Created by AllanXu on 01/03/2016.
 */
define(function (require) {
	var Vue = require('vue'),
		Config = require('config'),
		_ = require('ajax'),
		utilities = require('utilities'),
		id = sessionStorage.id;

	var app = new Vue({
		el: '#uc_lives',
		data: {
			req: {
				homeRank: '',
				maxConsumer: '',
				smoking: '',
				faith: '',
				sport: '',
				drink: '',
				rest: '',
				romantic: '',
				child: '',
				live: '',
				lifeSkills: ''
			}
		},
		methods: {
			init: function () {
				app.detail = utilities.base64TojsonObj(sessionStorage.userInfo).detail;

				for (var key in app.req) {
					app.req[key] = app.detail[key];
				}

				_(Config.DICTIONARY, {
					parentId: '660,668,678,728,686,694,700,705,712,717,747'
				}, function (data) {
					for(var key in data.obj){
						data.obj[key].unshift({id: '', name: '请选择'});
					}

					data.obj[747].splice(0,1);

					app.$set('homeRankList', data.obj[660]);
					app.$set('maxConsumerList', data.obj[668]);
					app.$set('smokingList', data.obj[678]);
					app.$set('faithList', data.obj[728]);
					app.$set('sportList', data.obj[686]);
					app.$set('drinkList', data.obj[694]);
					app.$set('restList', data.obj[700]);
					app.$set('romanticList', data.obj[705]);
					app.$set('childList', data.obj[712]);
					app.$set('liveList', data.obj[717]);

					var lifeSkillsList = data.obj[747];
					utilities.restoreCheckList(lifeSkillsList, app.req.lifeSkills.split(','));

					app.$set('lifeSkillsList', lifeSkillsList);
				});
			},
			save: function () {
				var req = {};
				for (var key in app.req) {
					req['xgdetail.' + key] = app.req[key];
				}

				req['xgdetail.lifeSkills'] = utilities.getCheckedStr(app.lifeSkillsList);

				req.id = id;
				console.log(req);

				_(Config.SERVICE.USERCENTER.LIVE_SAVE, req, function (data) {
					if (data.code == 200 && data.type === 'SUCCESS') {
						alert('保存成功！');
						utilities.refreshUserInfo(function (data) {
							history.back();
						});
					} else {
						alert(data.errmsg);
					}
				});
			},
			isChecked: function (item) {
				return !!item.checked;
			},
			pick: function (i) {
				app.$set('lifeSkillsList[' + i + '].checked', !app.lifeSkillsList[i].checked);
			},
			getName: function (arr, id) {
				for (var i = 0; i < arr.length; i++)
					if (arr[i]['id'] == id)
						return arr[i]['name'];
			}
		}
	});
	app.init();
});
